{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "operationList=['and','or','+','-','*'] #TODO:not, /\n",
    "class Operator:\n",
    "    def __init__(self,virable_nums) -> None:\n",
    "        self.calculate_expression='X[0]'\n",
    "        self.virable_nums=virable_nums\n",
    "        for i in range(self.virable_nums-1):\n",
    "            # get a random operator\n",
    "            random_operator = random.sample(operationList,1)[0]\n",
    "            self.calculate_expression += ' '+random_operator+' '+f'X[{i+1}]'\n",
    "        # 展示生成的表达式\n",
    "        self.show()\n",
    "    def calulate(self,X):\n",
    "        return eval(self.calculate_expression)\n",
    "    def show(self)-> None:\n",
    "        # X是计算的变量列表/向量\n",
    "        print('计算表达式为：',self.calculate_expression)\n",
    "        print('变量个数：',self.virable_nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.        ,   4.06122449,   8.12244898,  12.18367347,\n",
       "        16.24489796,  20.30612245,  24.36734694,  28.42857143,\n",
       "        32.48979592,  36.55102041,  40.6122449 ,  44.67346939,\n",
       "        48.73469388,  52.79591837,  56.85714286,  60.91836735,\n",
       "        64.97959184,  69.04081633,  73.10204082,  77.16326531,\n",
       "        81.2244898 ,  85.28571429,  89.34693878,  93.40816327,\n",
       "        97.46938776, 101.53061224, 105.59183673, 109.65306122,\n",
       "       113.71428571, 117.7755102 , 121.83673469, 125.89795918,\n",
       "       129.95918367, 134.02040816, 138.08163265, 142.14285714,\n",
       "       146.20408163, 150.26530612, 154.32653061, 158.3877551 ,\n",
       "       162.44897959, 166.51020408, 170.57142857, 174.63265306,\n",
       "       178.69387755, 182.75510204, 186.81632653, 190.87755102,\n",
       "       194.93877551, 199.        ])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,199)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算表达式为： X[0] or X[1] or X[2] - X[3] - X[4] + X[5] - X[6] and X[7] - X[8] + X[9]\n",
      "变量个数： 10\n"
     ]
    }
   ],
   "source": [
    "def datasets_generator(operation,x_len=10,data_point_num=100):\n",
    "    \"\"\"_summary_\n",
    "\n",
    "    Args:\n",
    "        x_len (int, optional): 运算个数. Defaults to 10.\n",
    "        data_point_num (int, optional): 数据集规模. Defaults to 100.\n",
    "\n",
    "    Returns:\n",
    "        _type_: _description_\n",
    "    \"\"\"\n",
    "    \n",
    "    # 用于生成数据点的辅助数据\n",
    "    X_meta =[i for i in range(200)]\n",
    "\n",
    "    #生成的数据集\n",
    "    X=[]\n",
    "    Y=[]\n",
    "    for i in range(data_point_num):\n",
    "        x = random.sample(X_meta,x_len)\n",
    "        y = operation.calulate(x)\n",
    "        X.extend(x)\n",
    "        Y.append(y)\n",
    "    X = np.array(X)\n",
    "    X.reshape([data_point_num,x_len])\n",
    "    \n",
    "    return X,Y\n",
    "\n",
    "# hidden law\n",
    "x_len=10\n",
    "operation = Operator(x_len)\n",
    "X,Y=datasets_generator(operation,x_len=x_len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 42, 188,  40, 135,   8,  31,  22, 162, 191, 173, 114, 143,  27,\n",
       "        112,  89, 170, 136,  10, 106, 140,   2, 168, 119,  70,  75,  64,\n",
       "        116,  38,  48,  13, 168, 180, 129,  51,  34, 188,  15,  20,  73,\n",
       "        122,  93,  44,  43,  39, 151,  25,  46, 190, 111,  77,  50, 164,\n",
       "        148, 114,  88, 101, 150,  17, 125, 103,  47, 137, 108,  83, 131,\n",
       "         18, 167, 143,  58, 180,  35,  97, 181,  73,  52, 190, 166,  38,\n",
       "        103,  62, 150, 192,  37, 113, 199,  21, 111,  51,   2,  45,  38,\n",
       "         88, 110,  54,  74, 131, 154, 161, 157, 152, 174,  54, 192, 137,\n",
       "         28,  77,  67, 104,  32, 164, 122,  50, 132,  73,  81,  35, 185,\n",
       "         78, 113,  38, 114, 169, 144, 100, 150,  73,  25, 104, 112, 121,\n",
       "         93,  45, 162, 166, 147, 186, 134,  34,  26, 157, 174, 130,  75,\n",
       "        163,  45, 194,  56,  98,  78, 171,  40, 109,   1, 153, 156, 166,\n",
       "        132,  62, 130,  26,  80,  79, 178, 169,  71,  16,  21,  74,   3,\n",
       "         72,  42, 133,  82,   3,  46, 134, 189,  23,  52, 150, 135,  84,\n",
       "        108, 137,   3,  55, 154,  91, 150,  51, 169,  56, 193,  40, 153,\n",
       "        167, 118, 114, 154, 102,  75,  68,  50, 155, 112,  88,  98,  62,\n",
       "        131,  76, 194,  68,   7,  46, 175,  51,  79,  77, 189, 171, 153,\n",
       "        133, 160,  11,  88,  94,  52, 118, 191, 132, 184,  97,  50,  82,\n",
       "         77, 152,   0, 107,  89, 121,  31,  97,   5,  77, 193, 172, 185,\n",
       "         12, 110,  46, 127, 105,  45, 148,  22, 146,  94,  18, 132, 172,\n",
       "         67, 105, 110,  76, 107, 148,  84,  97, 169,  56,  23, 184,  18,\n",
       "        197,  46,  13,   9, 193, 189, 123,  40, 112, 138,  66,  19, 188,\n",
       "         28,  62,  83,  23, 118, 180, 179,  57, 102,  29,  74,  96,  32,\n",
       "         78, 172, 125, 158, 118,  69, 178, 150,  66,   0,  29,  70,  79,\n",
       "        117, 182,  13, 168,   7,   6, 145,  68,  41, 100,  28,  52,  39,\n",
       "         58,  95,  83, 178,  12,  35,  76,  20,  39, 162,  18, 153,  55,\n",
       "        108,  99,  38,  55,  44,  14, 131, 111, 101,  49, 199,  70, 111,\n",
       "        158,   7, 172,  26, 107, 130, 118, 155, 154,  61, 160, 110,   9,\n",
       "         17,  83, 141, 128, 167,  88,  57,  79, 149, 138, 117, 137,   0,\n",
       "        107, 147, 148,  92,  10, 159, 177,  17, 127,  88, 128, 152,  59,\n",
       "          3, 187,  33, 199,   6, 107,  43,  76, 137, 156, 144,  82, 133,\n",
       "         65, 168,  76, 183,  39, 139, 108, 189,  80, 152,  59,  46,  95,\n",
       "        123,  12,  36, 111, 111,  76, 161, 142, 167,   4, 123,  50,  28,\n",
       "        179, 138,   1, 158,  17,  41,  98,  82,   5,  83, 190,  67, 195,\n",
       "        124, 177,  77, 102, 184, 135,  73, 194, 191, 151, 120,  94, 164,\n",
       "        173,  91,  70,  69, 145,  90,  32,  85, 139, 191, 119,  49, 121,\n",
       "         61,   7, 124,  18,  14,  45,  52,  60,  87, 101,  68, 130,  21,\n",
       "         49, 151,  28, 188,  19, 178,  17,  34, 120,  51,  37,  65, 161,\n",
       "         61,  48, 123,  19,  55,   9,  85, 134,   9,  95, 194, 196, 174,\n",
       "         65,  22, 121, 111, 120, 113,  32, 187,  84,  72,  96, 176, 188,\n",
       "        167, 135, 108,  40, 162, 140, 184,  77,  70, 171,  84, 125, 132,\n",
       "        119,  23, 195,  36, 176, 174, 118,   4,  12,  28, 180,  49,  84,\n",
       "         29, 100, 147, 124, 100, 114,  51,  73, 125, 126, 123, 163,  90,\n",
       "        117, 163, 171,  86, 151, 122,  94,  57, 124,  36,   8, 170,   9,\n",
       "         40,  47, 156, 129,  67,  39,  33,  42, 141, 120, 118,  37, 166,\n",
       "         75,   8, 188, 192,  69,  50, 119,  97, 146, 199, 125,  89,  88,\n",
       "        193, 170,  37, 161,  40, 134,  55,  39,  96,  45,  63, 136, 184,\n",
       "        188, 151,  85, 105, 126,  70, 192, 163, 111,  20,  56, 135, 101,\n",
       "        144, 149,  30,  15, 133,  89,  59, 143, 127,  93,  56,  36, 195,\n",
       "         58,  41, 104, 166, 122, 187,  67,  27,  47, 153,  21,   9, 188,\n",
       "        111,  66, 150,  16,  18, 151,  25,  40,  62, 163, 133, 121, 103,\n",
       "        106, 166, 119,  98,  90,  69,  87,  90, 132,  39, 192,  34,  59,\n",
       "        162,  23, 191,  24,  22, 114, 106, 164, 179,  75,  97, 147,  59,\n",
       "         71, 189, 163,  99,  54, 134,   4,  94, 169,  85,  45,  85,  41,\n",
       "        160, 140,  84,  52, 166, 142, 195,  59,  84, 182,   1,  36, 174,\n",
       "        110,  29, 196,  34, 105, 185,  27,  66, 104, 158,  26, 120,  56,\n",
       "         77,  15, 147,  69,  44,  42,  65, 137,  73, 191, 120,   3, 129,\n",
       "        119, 108,  43,  70, 180,  39,  23,  37,  61,  49,  73, 141, 183,\n",
       "         32, 121,  86, 108, 133, 124, 167, 148, 158,  73, 120, 132, 131,\n",
       "         68, 141,  46, 179,  27, 114,  30,  89, 187,  33, 155, 107,  31,\n",
       "         97, 198,  19,  95, 116, 135, 126, 138, 139,  50,  60,  69,  35,\n",
       "         87,  63,  78,  57,  11,   3, 115, 138,  87,  62, 153,   9,  54,\n",
       "         74,  85,  64,  72,  74,  69, 169,  57,  56, 186, 102, 124, 138,\n",
       "        187,  74, 132,  84, 198, 172, 188,  32,  92, 169,  94,  69,  42,\n",
       "         48, 155,  35,  65,  47, 199, 190, 175, 174, 192,  86, 114,  22,\n",
       "        169, 161, 185, 147,  93, 175,  11, 157,  97, 143, 179, 138, 108,\n",
       "        189,  38, 142,  65,  70,  17, 112, 101, 128,  27, 134, 106, 134,\n",
       "          2,   6,  36, 165, 151,  96,  81,  12, 130,  15, 133,   5, 149,\n",
       "        176, 157, 170,  61,  32,  30, 119, 190,  99,  36, 172,  87,  69,\n",
       "         26, 107,  76,   9, 193, 190,  40,  28, 102,  20,  56, 137,  65,\n",
       "        113,  21, 133,   2,  11,  81, 186,  17,  55,  31,  43, 128, 174,\n",
       "         67, 170,  42, 179, 184,  44, 139,  50,  12,  81, 164, 114, 125,\n",
       "        188,  25, 147, 167,  73, 112, 114,  27,  55,   1,  79, 122, 164,\n",
       "         76, 195, 187, 134,  24, 160, 151,  44,  76,  61, 161,  88, 120,\n",
       "        183, 184,  79,  63,  11,  68,  17, 186,  85,  39,  34, 191,  79,\n",
       "         52, 138,  20, 198,  37, 158,   0, 168, 107, 170,  10, 175,  29,\n",
       "         26,  82,  49,  41, 179, 127, 108, 186, 180,  95, 142,  62]),\n",
       " [42,\n",
       "  114,\n",
       "  2,\n",
       "  168,\n",
       "  93,\n",
       "  50,\n",
       "  47,\n",
       "  35,\n",
       "  150,\n",
       "  38,\n",
       "  174,\n",
       "  122,\n",
       "  114,\n",
       "  93,\n",
       "  174,\n",
       "  40,\n",
       "  80,\n",
       "  42,\n",
       "  135,\n",
       "  169,\n",
       "  75,\n",
       "  194,\n",
       "  153,\n",
       "  184,\n",
       "  31,\n",
       "  127,\n",
       "  67,\n",
       "  23,\n",
       "  40,\n",
       "  118,\n",
       "  172,\n",
       "  70,\n",
       "  41,\n",
       "  35,\n",
       "  38,\n",
       "  111,\n",
       "  61,\n",
       "  57,\n",
       "  92,\n",
       "  3,\n",
       "  144,\n",
       "  189,\n",
       "  111,\n",
       "  138,\n",
       "  67,\n",
       "  191,\n",
       "  90,\n",
       "  124,\n",
       "  21,\n",
       "  51,\n",
       "  85,\n",
       "  111,\n",
       "  167,\n",
       "  84,\n",
       "  4,\n",
       "  100,\n",
       "  163,\n",
       "  170,\n",
       "  141,\n",
       "  50,\n",
       "  37,\n",
       "  184,\n",
       "  20,\n",
       "  59,\n",
       "  166,\n",
       "  111,\n",
       "  133,\n",
       "  90,\n",
       "  22,\n",
       "  189,\n",
       "  85,\n",
       "  84,\n",
       "  185,\n",
       "  147,\n",
       "  129,\n",
       "  49,\n",
       "  167,\n",
       "  179,\n",
       "  97,\n",
       "  60,\n",
       "  138,\n",
       "  74,\n",
       "  74,\n",
       "  69,\n",
       "  174,\n",
       "  175,\n",
       "  142,\n",
       "  134,\n",
       "  15,\n",
       "  119,\n",
       "  9,\n",
       "  113,\n",
       "  43,\n",
       "  50,\n",
       "  73,\n",
       "  195,\n",
       "  88,\n",
       "  85,\n",
       "  158,\n",
       "  49])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X,Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('./x',X,allow_pickle=False)\n",
    "\n",
    "import pickle\n",
    "\n",
    "with open('Y.pkl', 'wb') as file:\n",
    "    pickle.dump(Y, file)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env4cv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
